Skip to content

feat(capture): add capture_mode config scaffolding (capture v1, 1/6)#701

Draft
eli-r-ph wants to merge 2 commits into
mainfrom
capture-v1/01-config
Draft

feat(capture): add capture_mode config scaffolding (capture v1, 1/6)#701
eli-r-ph wants to merge 2 commits into
mainfrom
capture-v1/01-config

Conversation

@eli-r-ph

Copy link
Copy Markdown

💡 Motivation and Context

First PR in a stacked series adding Capture V1 (POST /i/v1/analytics/events) support to the SDK, at parity with posthog-go's CaptureMode and posthog-rs's capture-v1.

This PR is pure, inert scaffolding: it adds the capture-mode selector and threads it everywhere it will be needed, but changes no runtime behavior — V0 (legacy /batch/) still runs on every path. Later PRs add the v1 serializer, transport, wiring, harness suite, and docs.

  • New CaptureMode enum: V0 (legacy /batch/, the default) and V1 (/i/v1/analytics/events).
  • resolve_capture_mode() with precedence explicit kwarg > POSTHOG_CAPTURE_MODE env > V0. Env accepts v0/legacy and v1/analytics_v1 (mirrors posthog-go's vocabulary); an unrecognized env value warns and defaults to V0 so a typo never silently flips the wire protocol. An invalid explicit kwarg raises ValueError (explicit misconfig should be loud).
  • Plumbed through Client.__init__ (resolved + stored as self.capture_mode), both Consumer(...) constructions (initial + fork-reinit), and the module-level default client. Consumer stores it but does not yet act on it.

💚 How did you test it?

  • New posthog/test/test_capture_mode.py (25 cases): resolution precedence, env aliases/case/whitespace, blank-env default, unrecognized-env warn+default, invalid-kwarg raises, and plumbing to Client.capture_mode + per-Consumer propagation (incl. multi-thread).
  • Regression: ran test_consumer, test_client, test_module, test_client_fork, test_dedicated_ai_endpoint + the new file — 213 passed.
  • ruff format/ruff check clean; mypy clean on new modules; regenerated references/public_api_snapshot.txt.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change (additive, default V0 keeps upgrades transparent).

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Authored with Cursor (Claude Opus 4.8) following a pre-agreed, hardened implementation plan. Design decisions: enum members are Pythonic V0/V1 (not posthog-go's Legacy/AnalyticsV1) but the env var accepts both spellings for cross-SDK consistency; the env typo path defaults defensively while an explicit bad kwarg fails fast. Mode is fully inert here by design to keep this PR a no-op for existing users and easy to review. Changeset (sampo add) and docs are intentionally deferred to the final PR so the feature releases as one entry.

@greptile-apps

greptile-apps Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "feat(capture): add capture_mode config s..." | Re-trigger Greptile

Comment thread posthog/test/test_capture_mode.py
@github-actions

github-actions Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-06-28 00:30:02 UTC
Duration: 530108ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 517ms
Format Validation.Event Has Uuid 10008ms
Format Validation.Event Has Lib Properties 10007ms
Format Validation.Distinct Id Is String 10007ms
Format Validation.Token Is Present 10007ms
Format Validation.Custom Properties Preserved 10008ms
Format Validation.Event Has Timestamp 10007ms
Retry Behavior.Retries On 503 18020ms
Retry Behavior.Does Not Retry On 400 12003ms
Retry Behavior.Does Not Retry On 401 10008ms
Retry Behavior.Respects Retry After Header 16014ms
Retry Behavior.Implements Backoff 30018ms
Retry Behavior.Retries On 500 13012ms
Retry Behavior.Retries On 502 16010ms
Retry Behavior.Retries On 504 16011ms
Retry Behavior.Max Retries Respected 30019ms
Deduplication.Generates Unique Uuids 7003ms
Deduplication.Preserves Uuid On Retry 16016ms
Deduplication.Preserves Uuid And Timestamp On Retry 23019ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 16006ms
Deduplication.No Duplicate Events In Batch 10002ms
Deduplication.Different Events Have Different Uuids 10007ms
Compression.Sends Gzip When Enabled 10008ms
Batch Format.Uses Proper Batch Structure 10007ms
Batch Format.Flush With No Events Sends Nothing 5005ms
Batch Format.Multiple Events Batched Together 10005ms
Error Handling.Does Not Retry On 403 12009ms
Error Handling.Does Not Retry On 413 10007ms
Error Handling.Retries On 408 14014ms

Feature_Flags Tests

16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 9502ms
Request Payload.Flags Request Uses V2 Query Param 10007ms
Request Payload.Flags Request Hits Flags Path Not Decide 10007ms
Request Payload.Flags Request Omits Authorization Header 10007ms
Request Payload.Token In Flags Body Matches Init 10007ms
Request Payload.Groups Round Trip 10007ms
Request Payload.Groups Default To Empty Object 10007ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 10007ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 10007ms
Request Payload.Disable Geoip Omitted Defaults To False 10008ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 10007ms
Request Lifecycle.No Flags Request On Init Alone 5003ms
Request Lifecycle.No Flags Request On Normal Capture 10508ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 9510ms
Request Lifecycle.Mock Response Value Is Returned To Caller 10003ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 10510ms

@eli-r-ph eli-r-ph force-pushed the capture-v1/01-config branch from 531fb62 to 33ebdfb Compare June 27, 2026 23:16
@eli-r-ph eli-r-ph changed the base branch from main to sdk-harness-audit-20260627-posthog-python June 27, 2026 23:17
@eli-r-ph eli-r-ph changed the base branch from sdk-harness-audit-20260627-posthog-python to main June 27, 2026 23:58
@eli-r-ph eli-r-ph force-pushed the capture-v1/01-config branch from 33ebdfb to b508cf9 Compare June 27, 2026 23:58
eli-r-ph added 2 commits June 27, 2026 17:10
Introduce a CaptureMode enum (V0 legacy /batch/, V1 /i/v1/analytics/events)
and resolve_capture_mode() with precedence kwarg > POSTHOG_CAPTURE_MODE env >
V0. Plumb capture_mode through Client and Consumer (including fork-reinit and
the module-level default client). The mode is resolved and stored but inert in
this change: V0 still runs everywhere, so behavior is unchanged.

First of a stacked series adding Capture V1 support.
Set the env to the opposite mode in each precedence row so every case
actually exercises kwarg-over-env, and assert an invalid kwarg raises
even when a valid env value is present.
@eli-r-ph eli-r-ph force-pushed the capture-v1/01-config branch from b508cf9 to b139d02 Compare June 28, 2026 00:20
@eli-r-ph eli-r-ph self-assigned this Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant